//
//  SellCarViewController.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/9/14.
//

import UIKit

class SellCarViewController: BaseViewController {
    var messageBtn: UIButton!
    var newMesRed: UIView!//信息红点
    var tableView: BaseTableView!
    var dataModel: FreeQuoteModel!
    var stepDiagramArray = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initNav()
        self.view.backgroundColor = Theme.F5Color
        self.inintTableView()
        self.dealStepDiagramData()
        self.dataModel = FreeQuoteModel.init()
        NotificationCenter.default.addObserver(self, selector: #selector(reloadDataModel), name: NSNotification.Name("freeQuoteSucceed"), object: nil)
    }
    
  
    
    func initNav(){
        let titleLab = UILabel(frame: CGRect(x: 18, y: NavBarHight - 40, width: 220, height: 30))
        titleLab.textColor = Theme.titleLabColor
        titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        titleLab.text = MMLocalizedString("get_ree_quote", comment: "获取免费报价")
        titleLab.backgroundColor = .white
        self.navBar.addSubview(titleLab)
        self.navBar.wr_setBottomLineHidden(hidden: true)
        
        titleLab.sizeToFit()
        titleLab.frame.size.width = titleLab.frame.size.width + 20
        
        
        self.messageBtn = UIButton(frame: CGRect(x: kScreenWidth - 75, y: NavBarHight - 40, width: 30, height: 30))
        self.messageBtn.setImage(UIImage(named: "icon_news_msg"), for: .normal)
        self.messageBtn.contentMode = .scaleAspectFill
        self.messageBtn.addTarget(self, action: #selector(messageBtnAtion), for: .touchUpInside)
        self.navBar.addSubview(self.messageBtn)
        
        
        self.newMesRed = UIView(frame: CGRect(x: self.messageBtn.frame.size.width - 12, y: 4, width: 4, height: 4))
        self.newMesRed.backgroundColor = Theme.redColor
        self.newMesRed.layer.cornerRadius = 2
        self.newMesRed.layer.masksToBounds = true
        self.newMesRed.isHidden = true
        self.messageBtn.addSubview(self.newMesRed)
        
        let setBtn = UIButton(frame: CGRect(x: kScreenWidth - 42, y: NavBarHight - 40, width: 30, height: 30))
        setBtn.setImage(UIImage(named: "icon_news_set"), for: .normal)
        setBtn.addTarget(self, action: #selector(setBtnAntion), for: .touchUpInside)
        self.navBar.addSubview(setBtn)
    }
    
    func inintTableView(){
        self.tableView = BaseTableView(frame: CGRect(x: 0, y: NavBarHight, width: kScreenWidth, height: kScreenHeight - NavBarHight - TabbarHeight), style: .grouped)
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 2
        self.tableView.backgroundColor = .clear
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(StepDiagramTableViewCell.self, forCellReuseIdentifier: "StepDiagramTableViewCell")
        self.tableView.register(NextStepTableViewCell.self, forCellReuseIdentifier: "NextStepTableViewCell")
        self.view.addSubview(self.tableView)
    }
    
    //步骤说明图
    func dealStepDiagramData(){
        
        let dataModel = VideoVRPdfModel.init()
        dataModel.name = MMLocalizedString("car_platform", comment: "1、在Kool Car平台登记车辆资料")
        dataModel.image = "icon_step"
        dataModel.content = MMLocalizedString("provide_photos", comment: "·尽量提供相片。")
        dataModel.content1 = MMLocalizedString("accurate_the_quotation", comment: "·你提供的资料越详细，报价越准确。")
        dataModel.content2 = ""
        
        let dataModelOne = VideoVRPdfModel.init()
        dataModelOne.name = MMLocalizedString("provides_quotation", comment: "2、销售顾问提供报价")
        dataModelOne.image = "icon_step_one"
        dataModelOne.content = MMLocalizedString("model_the_market", comment: "·帮你比较市场上相同车辆型号的市场价格。")
        dataModelOne.content1 = MMLocalizedString("day_quote_guarantee", comment: "·24小时内提供最高报价和15天报价保证。")
        dataModelOne.content2 = ""
        
        let dataModelTwo = VideoVRPdfModel.init()
        dataModelTwo.name = MMLocalizedString("close_the_deal", comment: "3、销售顾问帮你完成交易")
        dataModelTwo.image = "icon_step_Two"
        dataModelTwo.content = MMLocalizedString("car_inspection", comment: "·联络你到车行验车。")
        dataModelTwo.content1 = MMLocalizedString("transaction_procedures", comment: "·帮你完成全部交易手续。")
        dataModelTwo.content2 = MMLocalizedString("express_transfer", comment: "·安排特快转帐。")
        
        self.stepDiagramArray.add(dataModel)
        self.stepDiagramArray.add(dataModelOne)
        self.stepDiagramArray.add(dataModelTwo)
        
        
        
    }
    
 
    //MARK: - Antion
     func jumpToLogin(){
        guard let tabbarVC = UIApplication.shared.keyWindow?.rootViewController as? BaseTabbarController else {
            return
        }
        let vc = LoginViewController()
        let loginNavi = BaseNavigationController.init(rootViewController: vc)
        loginNavi.modalPresentationStyle = .fullScreen
        tabbarVC.present(loginNavi, animated: true)
    }
    
    
    //信息
   @objc func messageBtnAtion(){
       if UserModule.shared.isLogined == false{
           self.jumpToLogin()
           return
       }
       let vc = MessageViewController()
       self.navigationController?.pushViewController(vc, animated: true)
    }
    
    //个人中心
    @objc func setBtnAntion(){
        if UserModule.shared.isLogined == false{
            self.jumpToLogin()
            return
        }
        let vc = UserCenterViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func reloadDataModel(){
        self.dataModel = FreeQuoteModel.init()
        self.tableView.reloadData()
    }
    
}

extension SellCarViewController: UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
     
        return UITableViewAutomaticDimension
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "StepDiagramTableViewCell", for: indexPath) as! StepDiagramTableViewCell
            if self.stepDiagramArray.count > 0 {
                cell.setDataArray(dataArray: self.stepDiagramArray as! [VideoVRPdfModel])
            }
            return cell
       
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: "NextStepTableViewCell", for: indexPath) as! NextStepTableViewCell
            cell.delegate = self
            if self.dataModel != nil{
                cell.setModel(model: self.dataModel)
            }
            return cell
            
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0.01))
          headerView.backgroundColor = .clear
        return headerView
    }
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0.01))
        footerView.backgroundColor = .clear
        return footerView
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
}

extension SellCarViewController: NextStepTableViewCellDelegate{
    func textFieldDidBrandModel(brand: String, model: String) {
        self.dataModel.carBrand = brand
        self.dataModel.carStyle = model
        if self.dataModel.carBrand != nil && self.dataModel.carBrand != "" {
            self.dataModel.emptyBrand = ""
        }
        if self.dataModel.carStyle != nil && self.dataModel.carStyle != ""{
            self.dataModel.emptyModel = ""
        }
        self.tableView.reloadData()
    }
    
    
    func selectNextStepAntion(selectInex: NSInteger) {
        self.view.endEditing(true)
        if selectInex == 50 {
            self.didSelectCarDate()
        }else{    //下一步
            
            if UserModule.shared.isLogined == false{
                self.jumpToLogin()
                return
            }
            
            if self.dataModel.carBrand == nil || self.dataModel.carBrand == "" {
                self.dataModel.emptyBrand = MMLocalizedString("please_enter_vehicle_brand", comment: "请输入车辆品牌")
            }else{
                self.dataModel.emptyBrand = ""
            }
            
            if self.dataModel.carStyle == nil || self.dataModel.carStyle == ""{
                self.dataModel.emptyModel = MMLocalizedString("please_enter_vehicle_model_number", comment: "请输入车辆型号")
            }else{
                self.dataModel.carStyle = ""
            }
            
            if self.dataModel.carDate == nil || self.dataModel.carDate == ""{
                self.dataModel.emptyYear = MMLocalizedString("please_select_manufacture", comment: "请选择车辆制造年份")
            }else{
                self.dataModel.emptyYear = ""
            }
        
            self.tableView.reloadData()
            
            if self.dataModel.carBrand != nil && self.dataModel.carStyle != nil && self.dataModel.carDate != nil{
                let vc = FreeQuoteViewController()
                vc.dataModel = self.dataModel
                self.navigationController?.pushViewController(vc, animated: true)
            }
        
        }
    }

    ///年份
    func didSelectCarDate() {
        let vc = YearSelectViewController()
        vc.curYear = self.dataModel.carDate ?? ""
        vc.modalPresentationStyle = .overFullScreen
        vc.callBack = {[weak self] year in
            self?.dataModel.carDate = year
            if self?.dataModel.carDate != nil || self?.dataModel.carDate != ""{
                self?.dataModel.emptyYear = ""
            }
            self?.tableView.reloadData()
        }
        self.present(vc, animated: false)
        
//        let vc = MHPickerSheetView()
//        vc.modalPresentationStyle = .overFullScreen
//        vc.titleStr = MMLocalizedString("year_manufacture", comment: "制造年份")
//        vc.dataArray = XTManageCenter.getCarCreateDate()
//        if self.dataModel.carDate == "" {
//            //默认选中2020年
//            vc.selectItem = "2020"
//        }else{
//            vc.selectItem = self.dataModel.carDate
//        }
//        vc.callBack = {[weak self] (brandItem, _, configFlag) in
//            guard configFlag else {
//                //取消
//                return
//            }
//            self?.dataModel.carDate = (brandItem as! String)
//            if self?.dataModel.carDate != nil || self?.dataModel.carDate != ""{
//                self?.dataModel.emptyYear = ""
//            }
//            self?.tableView.reloadData()
//
//        }
//        self.present(vc, animated: false, completion: nil)
    }
    
    
}
extension SellCarViewController: UIScrollViewDelegate {
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.view.endEditing(true)
        self.view.resignFirstResponder()
    }
}
